package itheima_edu_info_manager.com.itheima.edu.info.manager.controller;

import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Student;
import itheima_edu_info_manager.com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();
    //创建学生菜单
    public final void start() {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    upDateStudentById();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理系统");
                    break studentLoop;
                default:
                    System.out.println("输入的选择有误，请重新输入！");
                    break;
            }
        }
    }

    //修改学生
    public final void upDateStudentById() {
        String id;
        id = putStudentId(true);
        Student stu = getStudent(id);

        studentService.upDateStudentById(id, stu);
        System.out.println("修改成功");
    }

    //将接收到的学生信息，进行封装
    public abstract Student getStudent(String id);

    //判断学生id是否存在
    public final String putStudentId(boolean flag) {
        String id;
        while (true) {
            System.out.println("请输入id：");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (exists == flag) {
                break;
            } else {
                System.out.println("输入的id无效，请重新输入！");
            }
        }
        return id;
    }

    //删除学生
    public final void deleteStudentById() {
        Student[] stu = studentService.findAllStudent();
        int index = -1;

        if (stu == null){
            System.out.println("无信息，请先添加后再操作");
            return;
        }else {
            System.out.println("请输入你需要删除的id");
            String id = sc.next();
            for (int i = 0; i < stu.length; i++) {
                Student student = stu[i];
                if (student.getId().equals(id)){
                    index = i;
                    break;
                }
            }
            if (index == -1){
                System.out.println("输入的id无效，请查询后重新输入");
                return;
            }else {
                Student student = stu[index];
                System.out.println("确认是否需要删除" + student.getId() + "的老师信息吗？y or n");
                System.out.println("学号\t\t姓名\t年龄\t生日");
                System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getBirthday());
                String choice = sc.next();
                if (choice.equals("y")){
                    studentService.deleteStudentById(id);
                    System.out.println("删除成功！");
                    return;
                }else {
                    System.out.println("手别抖。。");
                    return;
                }
            }
        }
    }

    //查询学生
    public final void findAllStudent() {
        Student[] student = studentService.findAllStudent();
        if (student == null) {
            System.out.println("查无信息，请添加后再进行查询！");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < student.length; i++) {
                Student st = student[i];
                if (st != null) {
                    System.out.println(st.getId() + "\t" + st.getName() + "\t" + st.getAge() + "\t\t" + st.getBirthday());
                }
            }
        }
    }

    //添加学生
    public final void addStudent() {
        String id;
        id = putStudentId(false);
        Student stu = getStudent(id);

        boolean result = studentService.addStudent(stu);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
